import { observable, action, toJS } from 'mobx';
import {WeaTableNew, WeaForm} from 'comsMobx'
const {TableStore} = WeaTableNew;
import {Modal, message} from "antd"
import {WeaLocaleProvider} from 'ecCom';
import * as Apis from '../apis/info';
const getLabel = WeaLocaleProvider.getLabel;

class SearchStore {

    @observable condition = [];         //搜索的condition
    @observable form = new WeaForm();   //搜索的form
    @observable tableStore = new TableStore();

    @observable showSearchAd = false;   //高级搜索显隐
    @observable isRight = false;        //判断是否有权限
    @observable loadingSpin = true;     //全局loading

    @observable status = {
        loading: false,
    }

    @action
    initDatas = (params = {}) => {
        this.status.loading = true;
        const newParams = {...params };
        this.condition = [];
        this.form = new WeaForm();
        this.loadingSpin = true;
        Apis.getInfoSearchCondition(newParams).then(data => {
            this.loadingSpin = false;
            this.isRight = data.isRight;
            if (!this.isRight) {
                return;
            }
            this.status.loading = false;
            this.condition = data.infoConditionGroup;
            this.form && !this.form.isFormInit && this.form.initFormFields(data.infoConditionGroup);
        })
    }

    @action
    doSearch = (params = {}, type) => {
        this.status.loading = true;
        //获取表单的参数值
        const searchParamsAd = this.form.getFormParams();
        const newParams = {...searchParamsAd };
        this.loadingSpin = true;
        Apis.getInfoSearchList(newParams).then(data => {
            this.loadingSpin = false;
            this.isRight = data.isRight;
            if (!this.isRight) {
                return;
            }
            this.tableStore = new TableStore();
            this.tableStore.getDatas(data.sessionkey, 1);
            this.status.loading = false;
        })
    }

    @action
    reLoad = () => {
        this.tableStore.getDatas(this.tableStore.dataKey, this.tableStore.current, this.tableStore.pageSize, this.tableStore.sortParams)
    }
    
    clearStatus = () => {
        this.condition = [];
        this.clearFormFields();    
        this.tableStore = new TableStore();
    }

    clearFormFields = () =>{
        this.form.resetConditionValue();      //清除查询条件值
    }
    
    setShowSearchAd = (bool) => {
        this.showSearchAd = bool;
    }

    appendFormFields = (value) => {
        this.form.updateFields(value, false);
    }

}


export default SearchStore;